/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 沈小白 2020-07-22 $
 */<template> 
	<view class="content">
		<image class="cart-image" :src="imgUrl+'user/get_user_info.png'"  role="img"></image>
		<view class="no-data">
			<button @getuserinfo="wxGetUserInfo" class="getuse_info" open-type="getUserInfo" role="button" aria-disabled="false">
				立即登录

			</button>
		</view>
	</view>
</template>

<script>
	const token_type= "bearer";
	import config from "@/api/config.js";
	import common from "@/common/common.js";
	import {
		wxlogin
	} from "@/api/api.js";
	export default {
		data() {
			return {
				hostUrl: config.host, //图片路径前缀
				imgUrl:config.imgUrl,
				SessionKey: '',
				userInfo:uni.getStorageSync('userInfo') || null,//用户信息
				loginInfo:uni.getStorageSync('loginInfo') || null,//登录信息
				OpenId: '',
				nickName: null,
				avatarUrl: null,
				isCanUse: uni.getStorageSync('isCanUse') || true //默认为true
			};
		},
		onLoad(){
			
		},
		methods: {
			//第一授权获取用户信息===》按钮触发
			wxGetUserInfo() {
				let _this = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						console.log("第一次", infoRes)
						let nickName = infoRes.userInfo.nickName; //昵称
						let avatarUrl = infoRes.userInfo.avatarUrl; //头像
						let userInfo={
							nickName:nickName,
							avatarUrl:avatarUrl
						}
						uni.setStorageSync("userInfo",userInfo)
						try {
							uni.setStorageSync('isCanUse', false); //记录是否第一次授权  false:表示不是第一次授权
							_this.login();
						} catch (e) {}
					},
					fail(res) {}
				});
			},

			//登录
			login() {
				let _this = this;
				// uni.showLoading({
				//     title: '登录中...'
				// });

				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						let code = loginRes.code;

						if (!_this.isCanUse) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									//获取用户信息后向调用信息更新方法
									console.log("第二次") 
									let nickName = infoRes.userInfo.nickName; //昵称
									let avatarUrl = infoRes.userInfo.avatarUrl; //头像
									let userInfo={
										nickName:nickName,
										avatarUrl:avatarUrl
									}
									// uni.setStorageSync("userInfo",userInfo)
								}
							});
						}

						//2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						let params = {
							js_code: code,
							test:7
						}
						console.log(params)
						_this._wxlogin(params)
						
						
					},
				});
			},
			
		
		_wxlogin(params) {
			wxlogin(params).then(res => {
				console.log(res)
				let loginInfo={
					token:token_type+res.token,
				}
				console.log(loginInfo)
				if(res){
					uni.setStorageSync('loginInfo', loginInfo)
					//检测是否获取到userInfo
					this.loginInfo=loginInfo;
					if(this.loginInfo){
						common.toast("none","登录成功",2000)
						uni.switchTab({
							url:"/pages/user/user/user"
						})
						
					}
				}else{
					common.toast("none",res.msg,2000)
				}
				
			})
			
		},
		
		
		
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		width: 100%;
		height: 100%;
		background-color: #f5f5f5;
	}
	.content{
	display: flex;
	width: 100%;
	height: 100%;	
	}
	.cart-image{
		display: flex;
		width: 100%;
		height: 100%;
	}
	.no-data {
		position: fixed;
		bottom: 30%;
	}

	.container {
		display: flex;
		height: 100%;
		align-items: center;
		/*垂直居中*/
		justify-content: center;
		/*水平居中*/
		background-color: #fff;
		padding-bottom: 100rpx;
	}

	.login-in {
		width: 100%;
		box-sizing: border-box;
	}

	.order-item {
		display: flex;
		justify-content: space-around;
		height: 200rpx;
		padding: 10rpx 0;
		border-bottom: 1px solid #f3f3f3;
	}

	.goods-ico {
		display: flex;
		align-items: center;
	}

	.goods-img {
		width: 180rpx;
		height: 180rpx;
		font-size: 0;
		margin-top: 10rpx;
	}

	.goods-cont {
		position: relative;
		width: 430rpx;
		height: 200rpx;
		font-size: 28rpx;
	}

	.goods-name {
		width: 370rpx;
		height: 64rpx;
		margin: 8rpx 0;
		line-height: 32rpx;
		color: #333;
	}

	.goods-attribute {
		max-height: 52rpx;
		line-height: 26rpx;
		font-size: 24rpx;
		color: #999;
	}

	.goods-price {
		height: 40rpx;
		line-height: 40rpx;
		margin-top: 10rpx;
	}

	.btn-del {
		position: absolute;
		right: 0;
		top: 8rpx;
		width: 34rpx;
		height: 34rpx;
	}

	.count {
		position: absolute;
		right: 0;
		bottom: 5rpx;
		width: 156rpx;
		height: 50rpx;
		border: 1px solid #f3f3f3;
	}

	.count>view,
	.count>input {
		float: left;
		width: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
	}

	.sub {
		border-right: 1px solid #f3f3f3;
	}

	.add {
		border-left: 1px solid #f3f3f3;
	}

	.pay-for {
		width: 100%;
		height: 100rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		font-size: 28rpx;
		color: #666;
		background-color: #f0f2f5;
	}

	.pay-for .pay-btn {
		float: right;
		width: 200rpx;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background-color: #ff3b3b;
		color: #fff;
		border-radius: 0;
	}

	.pay-for .consumer {
		float: right;
		margin-right: 20rpx;
		line-height: 50rpx;
	}

	.pay-for .checkbox {
		float: left;
		padding-left: 40rpx;
		line-height: 100rpx;
		display: flex;
		align-items: center;
	}

	.pay-for .ico-check {
		display: flex;
		align-items: center;
		margin-right: 20rpx;
	}

	.getuse_info {
		width: 50%;
		background-color: #fc4646;
		color: #fff;
		border: 1px solid #fff;
		border-radius: 50rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 36rpx;

	}

	.similer {
		margin: 0 auto;
		margin-top: 20px;
		width: 60%;
		height: 64rpx;
		line-height: 64rpx;
		font-size: 28rpx;
		border-radius: 10rpx;
		border: 1px solid #f3f3f3;
	}
</style>
